package com.reguang.service.impl;

import org.apache.commons.lang.ArrayUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import com.reguang.mapper.SysDeptMapper;
import com.reguang.mapper.SysUserMapper;
import com.reguang.pojo.SysDeptDO;
import com.reguang.pojo.SysMenuDO;
import com.reguang.pojo.SysUserDO;
import com.reguang.service.SysDeptService;
import com.reguang.utils.BuildTree;
import com.reguang.utils.Tree;

@Service
public class SysDeptServiceImpl implements SysDeptService {

	@Autowired
	private SysDeptMapper deptMapper;

	@Autowired
	private SysUserMapper sysUserMapper;

	@Override
	public SysDeptDO queryOne(SysDeptDO record) {
		return deptMapper.selectOne(record);
	}

	@Override
	public List<SysDeptDO> list(Map<String, Object> params) {
		// 设置分页
//		PageHelper.startPage(Integer.parseInt(params.get("offset").toString()),
//				Integer.parseInt(params.get("limit").toString()));
		List<SysDeptDO> lists = deptMapper.selectAll();
//		return new PageInfo<SysDeptDO>(list);
		return lists;
	}

	@Override
	public SysDeptDO queryByPrimaryKey(Integer id) {
		return deptMapper.selectByPrimaryKey(id);
//		return deptMapper.select(id);
	}

	@Override
	public Integer save(SysDeptDO dept) {
		return deptMapper.insert(dept);
	}

	@Override
	public Integer update(SysDeptDO dept) {
		return deptMapper.updateByPrimaryKey(dept);
	}

	@Override
	public Integer deleteById(Integer id) {
		SysDeptDO deptDo = new SysDeptDO();
		deptDo.setParentId(id + "");
		List<SysDeptDO> subList = deptMapper.select(deptDo);
		if (subList != null) {
           for(int i=0;i<subList.size();i++) {
        	   deptMapper.deleteByPrimaryKey(subList.get(i).getId());
           }
		}
		return deptMapper.deleteByPrimaryKey(id);
	}

	@Override
	public Tree<SysDeptDO> getTree() {
		List<Tree<SysDeptDO>> trees = new ArrayList<Tree<SysDeptDO>>();
		List<SysDeptDO> depts = deptMapper.selectAll();
		Integer[] pDepts = deptMapper.listParentDept();
		Integer[] uDepts = sysUserMapper.listAllDept();
		Integer[] allDepts = (Integer[]) ArrayUtils.addAll(pDepts, uDepts);
		for (SysDeptDO dept : depts) {
			if (!ArrayUtils.contains(allDepts, dept.getId())) {
				continue;
			}
			Tree<SysDeptDO> tree = new Tree<SysDeptDO>();
			tree.setId(dept.getId() + "");
			tree.setParentId(dept.getParentId().toString());
			tree.setText(dept.getName());
			Map<String, Object> state = new HashMap<>(16);
			state.put("opened", true);
			state.put("mType", "dept");
			tree.setState(state);
			trees.add(tree);
		}
		List<SysUserDO> users = sysUserMapper.selectAll();
		for (SysUserDO user : users) {
			Tree<SysDeptDO> tree = new Tree<SysDeptDO>();
			tree.setId(user.getId());
			tree.setParentId(user.getDeptId().toString());
			tree.setText(user.getName());
			Map<String, Object> state = new HashMap<>(16);
			state.put("opened", true);
			state.put("mType", "user");
			tree.setState(state);
			trees.add(tree);
		}
		// 默认顶级菜单为０，根据数据库实际情况调整
		Tree<SysDeptDO> t = BuildTree.build(trees);
		return t;
	}
}
